﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class AccountTypeRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "AccountTypeRenderBll";
            }
        }

        public override string Title
        {
            get
            {
                return "Loại nhóm tài khoản";
            }
        }


        
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
    

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawAddForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string ParentAccountType = (string)WebEnvironments.Request(SiteId, "pat");
                if (string.IsNullOrEmpty(ParentAccountType)) ParentAccountType = "";
                string Html =
                   " <script language=javascript>\r\n"+
                   "    function ClientBack()\r\n"+
                   "    {\r\n"+
                   "        ParentAccountType = document.getElementById('txtParentAccountType').value;\r\n" +
                   "        window.open('RoleMan.aspx?type='+ParentAccountType,'_self');\r\n"+
                   "    }\r\n"+

                   "    function ClientAddAccountType()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        ParentAccountType = document.getElementById('txtParentAccountType').value;\r\n" +
                   "        Description = document.getElementById('txtDescription').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.AccountTypeRenderBll.ServerSideAdd(SiteId, ParentAccountType, Description).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   
                   "        ClientBack();\r\n" +
                   "    }\r\n" +

                   " </script>\r\n"+
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientAddAccountType();\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "            <li onclick=\"javascript:ClientBack();\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <input id=txtParentAccountType type=hidden value=\""+ParentAccountType+"\">\r\n"+
                   "  <div class=CssFormSaveUpdate>\r\n"+
                   "  <h3>Thêm mới nhóm đối tượng</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên nhóm phân loại:</td>\r\n" +
                   "        <td class=Col2><input id=txtDescription class=textbox type=textbox></td>\r\n" +
                   "    </tr>\r\n"+
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawUpdateForm(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string AccountType = (string)WebEnvironments.Request(SiteId, "AccountType");
                AccountTypeCls OAccountType = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateAccountTypeModel(SiteId, AccountType);
                string Html =
                   " <script language=javascript>\r\n" +
                   "    function ClientBack(AccountType)\r\n" +
                   "    {\r\n" +
                   "        window.open('RoleMan.aspx?type='+AccountType,'_self');\r\n" +
                   "    }\r\n" +

                   "    function ClientSaveAccountType(AccountType)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Description = document.getElementById('txtDescription').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.AccountTypeRenderBll.ServerSideSaveAccountType(SiteId, AccountType, Description).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +

                   "        ClientBack(AccountType);\r\n" +
                   "    }\r\n" +

                   " </script>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "    <ul>\r\n" +
                   "        <li onclick=\"javascript:ClientSaveAccountType('" + AccountType + "');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                   "        <li onclick=\"javascript:ClientBack('"+AccountType+"');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "    </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div class=CssFormSaveUpdate>\r\n" +
                   "  <h3>Sửa nhóm truy cập</h3>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=Col1>Tên nhóm:</td>\r\n" +
                   "        <td class=Col2><input id=txtDescription class=textbox type=textbox value=\"" + OAccountType.Description + "\"></td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideAdd(string SiteId, string ParentAccountType, string Description)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(Description)) throw new Exception("Chưa nhập mô tả nhóm!");
                string AssetCode = WebSessionUtility.GetCurrentLoginUser().AssetCode;
                if (ParentAccountType.Equals("root")) ParentAccountType = null;
                string AccountType = System.Guid.NewGuid().ToString();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddAccountType(SiteId, ParentAccountType, AccountType, Description);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSaveAccountType(string SiteId, string AccountType, string Description)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                if (string.IsNullOrEmpty(AccountType)) throw new Exception("Chưa nhập mã nhóm!");
                if (string.IsNullOrEmpty(Description)) throw new Exception("Chưa nhập mô tả!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveAccountType(SiteId, AccountType, Description);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

    }
}
